<?php

namespace App\Http\Middleware;

use Closure;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
use Tymon\JWTAuth\Http\Middleware\BaseMiddleware;

class Auth extends BaseMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $response = $next($request);
        return $this->setAuthenticationHeader($response, $this->auth->getToken());

        try {
            $this->authenticate($request);
        } catch (UnauthorizedHttpException $e) {
            return response()->json(['message' => 'jwt 无效'], 401);
        }

        return $next($request);
    }
}
